package model;

import java.util.Iterator;

import net.sf.vcard4j.java.VCard;
import net.sf.vcard4j.java.type.ADR;
import net.sf.vcard4j.java.type.EMAIL;
import net.sf.vcard4j.java.type.FN;
import net.sf.vcard4j.java.type.N;
import net.sf.vcard4j.java.type.TEL;

public class VCardWrapper {
	
	private VCard vcard;
	
	public VCardWrapper(VCard vcard) throws Exception {
		if(vcard == null) throw new Exception("no vcard given!");
		this.vcard = vcard;
	}
	
	protected VCardWrapper() {vcard = null;}
	
	private Iterator getTypes(String typeName) {
		if (vcard == null) return null;
		Iterator iter = vcard.getTypes(typeName);
		if(iter == null || iter.hasNext() == false) return null;
		return iter;
	}
	
	public boolean existsCompanyName() {
		if(getCompanyName() == null || getCompanyName().trim().length() == 0) return false;
		return false;
	}
	
	public String getCompanyName() {
		Iterator iter = this.getTypes("FN");
		FN fn = (FN) iter.next();
		if(fn == null) 
			return null;
		else
			return fn.get();
	}
	
	public void setCompanyName(String name) {
		Iterator iter = this.getTypes("FN");
		FN fn = (FN) iter.next();
		if(fn == null) 
			return;
		else
			fn.set(name);
	}
	
	public boolean existsPreName() {
		if(getPreName() == null || getPreName().trim().length() == 0) return false;
		return true;
	}	
	
	public String getPreName() {
		Iterator iter = this.getTypes("N");
		N n = (N) iter.next();
		if(n == null) 
			return null;
		else
			return n.getGiven();
	}
	
	public void setPreName(String name) {
		Iterator iter = this.getTypes("N");
		N n = (N) iter.next();
		if(n == null)
			return;
		else
			n.setGiven(name);
	}
	
	public boolean existsLastName() {
		if(getLastName() == null || getLastName().trim().length() == 0) return false;
		return true;
	}		
	
	public String getLastName() {
		Iterator iter = this.getTypes("N");
		N n = (N) iter.next();
		if(n == null) 
			return null;
		else
			return n.getFamily();
	}
	
	public void setLastName(String name) {
		Iterator iter = this.getTypes("N");
		N n = (N) iter.next();
		if(n == null) 
			return;
		else
			n.setFamily(name);
	}
	
	public boolean existsEmail() {
		if(getEmail() == null || getEmail().trim().length() == 0) return false;
		return true;
	}
	
	public String getEmail() {
		Iterator iter = this.getTypes("EMAIL");
		EMAIL email = (EMAIL) iter.next();
		if(email == null) 
			return null;
		else
			return email.get();
	}
	
	public void setEmail(String emailaddr) {
		Iterator iter = this.getTypes("EMAIL");
		EMAIL email = (EMAIL) iter.next();
		if(email == null) 
			return;
		else
			email.set(emailaddr);
	}
	
	public boolean existsPhone() {
		if(getPhone() == null || getPhone().print().trim().length() == 0) return false;
		return true;
	}
	
	public PhoneNumberWrapper getPhone() {
		Iterator iter = this.getTypes("TEL");
		TEL phone = null;
		while(iter.hasNext()) {
			TEL currNumber = (TEL) iter.next();
			if( ((TEL.Parameters)currNumber.getParameters())
					.containsTYPE(TEL.Parameters.TYPE_HOME)) {
				phone = currNumber;
				break;
			}
		}
		if(phone == null) return null;
		return new PhoneNumberWrapper(phone);
	}
			
	public boolean existsFax() {
		if(getFax() == null || getFax().print().trim().length() == 0) return false;
		return true;
	}
	
	public PhoneNumberWrapper getFax() {
		Iterator iter = this.getTypes("TEL");
		TEL phone = null;
		while(iter.hasNext()) {
			TEL currNumber = (TEL) iter.next();
			if( ((TEL.Parameters)currNumber.getParameters())
					.containsTYPE(TEL.Parameters.TYPE_FAX)) {
				phone = currNumber;
				break;
			}
		}
		if(phone == null) return null;
		return new PhoneNumberWrapper(phone);
	}
	
	public boolean existsAddress() {
		if(getAddress() == null) return false;
		return true;
	}
	
	public AddressWrapper getAddress() {
		Iterator iter = this.getTypes("ADR");
		ADR adr = (ADR) iter.next();
		if(adr == null) 
			return null;
		else
			return new AddressWrapper(adr);
	}
	

}